package Controls;

import Core.ControlImage;
import Core.ALIGN;
import Core.Control;
import Core.Form;
import Core.GUICanvas;
import Core.Img;
import Core.LayoutManager;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class Button extends Control
{
	private Img a, i, p;
	private Text capt;

	public static int StandartIColor = 0xffffff;
	public static int StandartAColor = 0xbb0000;
	public static int StandartPColor = 0xbb0000;

	public static String StandartActivePath = "/light.cfg";
	public static String StandartInactivePath = "/dark.cfg";
	public static String StandartPressedPath = "/light.cfg";

	public Button(String caption, int w)
	{
		super(caption, caption, w, Font.getDefaultFont().getHeight() + 3);

		int h = Font.getDefaultFont().getHeight() + 3;

		a = GUICanvas.pool.CreateImage(StandartActivePath, w, h, Form.StandartBGColor, false);
		i = GUICanvas.pool.CreateImage(StandartInactivePath, w, h, Form.StandartBGColor, false);
		p = GUICanvas.pool.CreateImage(StandartPressedPath, w, h, Form.StandartBGColor, false);

		selectedAction = "Press";

		capt = new Text(caption, W, false);
	}

	public Button(String caption, String apath, String ipath, String ppath)
	{
		super();

		a = new Img(apath);
		i = new Img(ipath);
		p = new Img(ppath);

		this.caption = caption;

		this.W = a.getW();
		this.H = a.getH();

		capt = new Text(caption, W, false);

		selectedAction = "Press";
	}

	public Button(String caption, int w, int h)
	{
		super(caption, caption, w, h);

		a = GUICanvas.pool.CreateImage(StandartActivePath, w, h, Form.StandartBGColor, false);
		i = GUICanvas.pool.CreateImage(StandartInactivePath, w, h, Form.StandartBGColor, false);
		p = GUICanvas.pool.CreateImage(StandartPressedPath, w, h, Form.StandartBGColor, false);

		selectedAction = "Press";

		capt = new Text(caption, W, false);
	}

	public Button(String caption, int w, int h, String ap, String ip, String pp)
	{
		super(caption, caption, w, h);

		a = GUICanvas.pool.CreateImage(ap, w, h, Form.StandartBGColor, false);
		i = GUICanvas.pool.CreateImage(ip, w, h, Form.StandartBGColor, false);
		p = GUICanvas.pool.CreateImage(pp, w, h, Form.StandartBGColor, false);

		selectedAction = "Press";

		capt = new Text(caption, W, false);
	}

	public Button(String caption, LayoutManager m)
	{
		this.caption = caption;
		int w=0, h=0;

		if(m.type == LayoutManager.HORIZONTAL)
		{
			w = Font.getDefaultFont().stringWidth(caption)+8;
			h = Font.getDefaultFont().getHeight()+3;
		}
		else if(m.type == LayoutManager.VERTICAL)
		{
			w = m.getAvailableW();
			h = Font.getDefaultFont().getHeight()+3;
		}

		W = w;
		H = h;

		a = GUICanvas.pool.CreateImage(StandartActivePath, w, h, Form.StandartBGColor, false);
		i = GUICanvas.pool.CreateImage(StandartInactivePath, w, h, Form.StandartBGColor, false);
		p = GUICanvas.pool.CreateImage(StandartPressedPath, w, h, Form.StandartBGColor, false);

		selectedAction = "Press";

		capt = new Text(caption, W, false);
	}

	public void setCaption(String caption)
	{
		super.setCaption(caption);
		if(caption.equals("Назад") || caption.equals("Back"))
		{
			caption = " ";
			addImage(ControlImage.BACK_C);
		}
		else removeImage(ControlImage.BACK_C);

		capt.setText(caption);

	}

	public void draw(Graphics g, int x, int y)
	{
		if(caption.equals("") && images.isEmpty()) return;

		int s = 0;

		if(state == ACTIVE)
		{
			a.draw(g, x, y);
			g.setColor(StandartAColor);
		}
		if(state == INACTIVE)
		{
			i.draw(g, x, y);
			g.setColor(StandartIColor);
		}
		if(state == PRESSED)
		{
			p.draw(g, x+1, y+1);
			g.setColor(StandartPColor);
			s=1;
		}
		
		//g.drawString(caption, s+x+(getW()-Font.getDefaultFont().stringWidth(caption))/2, s+y+(getH() - Font.getDefaultFont().getHeight())/2, Graphics.TOP | Graphics.LEFT);
		capt.color = g.getColor();
		capt.align = Graphics.HCENTER;
		capt.draw(g, x+s, y+(getH() - capt.getH())/2 + s);

		super.draw(g, x, y);
	}

	public boolean select(int what)
	{
		return super.select(what);
	}
}
